using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EchoFramework;

/// <summary>
/// 地图配置表
/// </summary>
public class MapPosCfg : Config
{
    /// <summary>
    /// 关卡
    /// </summary>
    [ConfigMark]
    public readonly int Level;
    
    /// <summary>
    /// 坐标类型
    /// </summary>
    [ConfigMark]
    public readonly int PosType;
    
    /// <summary>
    /// 坐标点
    /// </summary>
    [ConfigMark]
    public readonly string Pos;
    
    /// <summary>
    /// 目标点ID
    /// </summary>
    [ConfigMark]
    public readonly string TargetPosID;
    
    /// <summary>
    /// 条件
    /// </summary>
    [ConfigMark]
    public readonly string Condition;

    /// <summary>
    /// 计算方式
    /// </summary>
    [ConfigMark]
    public readonly string Method; 

    /// <summary>
    /// 坐标类型 枚举
    /// </summary>
    public PosTypeEnum PosTypeEnum => (PosTypeEnum) PosType;

    /// <summary>
    /// 坐标点 
    /// </summary>
    public Coordinate PosCoordinate {
        get
        {
            string[] posArr = Pos.Split(',');
            int x = Convert.ToInt32(posArr[0]);
            int y = Convert.ToInt32(posArr[1]);
            return new Coordinate(x, y);
        }
    }
    
    /// <summary>
    /// 返回指定关卡的所有点
    /// </summary>
    /// <param name="level">关卡</param>
    public static List<MapPosCfg> GetMapCfgListByLevel(int level)
    {
        List<MapPosCfg> allList = ConfigManager<MapPosCfg>.Instance.GetDataList();
        List<MapPosCfg> retList = new List<MapPosCfg>();
        foreach (MapPosCfg tempCfg in allList)
        {
            if (tempCfg.Level == level)
            {
                retList.Add(tempCfg);
            }
        }
        
        if (retList.Count == 0)
        {
            GameLogger.LogWarning($"MapPosCfg.xml 关卡{level}的点位为0");
        }
        return retList;
    }

    /// <summary>
    /// 返回目标点配置列表
    /// </summary>
    public List<MapPosCfg> GetTargetIDCfgList()
    {
        List<MapPosCfg> retCfgList = new List<MapPosCfg>();
        string[] targetPosIDArr = TargetPosID.Split('|');
        foreach (string idStr in targetPosIDArr)
        {
            if (string.IsNullOrEmpty(idStr))
                continue;
            int tempId = Convert.ToInt32(idStr);
            MapPosCfg tempCfg = ConfigManager<MapPosCfg>.Instance.GetData(tempId);
            if (tempCfg == null)
            {
                GameLogger.LogWarning($"MapPosCfg.xml ID:{tempId} 找不到");
                continue;
            }
            retCfgList.Add(tempCfg);
        }
        return retCfgList;
    }

    /// <summary>
    /// 返回玩家当前关卡初始位置
    /// </summary>
    /// <param name="level">关卡</param>
    /// <param name="initScore">初始得分</param>
    public static Coordinate GetPlayerInitCoor(int level, out int initScore)
    {
        initScore = 0;
        List<MapPosCfg> allList = ConfigManager<MapPosCfg>.Instance.GetDataList();
        foreach (MapPosCfg tempCfg in allList)
        {
            if (tempCfg.Level == level)
            {
                string[] arr = tempCfg.Method.Split(',');
                if (arr[0] == "init")
                {
                    initScore = Convert.ToInt32(arr[1]);
                    return tempCfg.PosCoordinate;
                }
            }
        }

        return null;
    }
    
    /// <summary>
    /// 返回目标坐标相对于自己的方向  只能水平|竖直方向
    /// </summary>
    /// <param name="coor">坐标点</param>
    public DirectionEnum GetTargetDir(Coordinate coor)
    {
        int xOffset = PosCoordinate.X - coor.X;
        if (xOffset < 0)
        {
            return DirectionEnum.Right;
        }
        else if (xOffset > 0)
        {
            return DirectionEnum.Left;
        }

        int yOffset = PosCoordinate.Y - coor.Y;
        if (yOffset < 0)
        {
            return DirectionEnum.Up;
        }
        else if (yOffset > 0)
        {
            return DirectionEnum.Down;
        }

        return DirectionEnum.Null;
    }

    /// <summary>
    /// 返回配置中的最大关卡
    /// </summary>
    public static int GetMaxLevel()
    {
        List<MapPosCfg> allList = ConfigManager<MapPosCfg>.Instance.GetDataList();
        int ret = 0;
        foreach (MapPosCfg tempCfg in allList)
        {
            ret = ret < tempCfg.Level ? tempCfg.Level : ret;
        }

        return ret;
    }
}
